package com.ruoyi.project.ledger.controller;

import java.io.*;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.LedgerSafetyProductionMeeting;
import com.ruoyi.project.ledger.service.ILedgerSafetyProductionMeetingService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 安全生产领导小组会议记录台账Controller
 *
 * @author ruoyi
 * @date 2020-04-14
 */
@RestController
@RequestMapping("/ledger/meeting")
public class LedgerSafetyProductionMeetingController extends BaseController
{
    @Autowired
    private ILedgerSafetyProductionMeetingService ledgerSafetyProductionMeetingService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询安全生产领导小组会议记录台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:meeting:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyProductionMeeting.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyProductionMeeting.setAreas(areas);
        ledgerSafetyProductionMeeting.setManagementCharacters(managementCharacters);
        startPage();
        List<LedgerSafetyProductionMeeting> list = ledgerSafetyProductionMeetingService.selectLedgerSafetyProductionMeetingList(ledgerSafetyProductionMeeting);
        return getDataTable(list);
    }

    /**
     * 导出安全生产领导小组会议记录台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:meeting:export')")
    @Log(title = "安全生产领导小组会议记录台账", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        // 判断是否是场站角色
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyProductionMeeting.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyProductionMeeting.setAreas(areas);
        ledgerSafetyProductionMeeting.setManagementCharacters(managementCharacters);
        List<LedgerSafetyProductionMeeting> list = ledgerSafetyProductionMeetingService.selectLedgerSafetyProductionMeetingList(ledgerSafetyProductionMeeting);
        int i=0;
        for (LedgerSafetyProductionMeeting meeting : list){
            meeting.setId(Long.valueOf(++i));
        }
        ExcelUtil<LedgerSafetyProductionMeeting> util = new ExcelUtil<LedgerSafetyProductionMeeting>(LedgerSafetyProductionMeeting.class);
        return util.exportExcel(list, "meeting");
    }

    /**
     * 获取安全生产领导小组会议记录台账详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('ledger:meeting:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ledgerSafetyProductionMeetingService.selectLedgerSafetyProductionMeetingById(id));
    }

    /**
     * 新增安全生产领导小组会议记录台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:meeting:add')")
    @Log(title = "安全生产领导小组会议记录台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting,@RequestBody String meetingPath)
    {
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerSafetyProductionMeeting.setStationId(stationId);
            ledgerSafetyProductionMeeting.setCreateId(String.valueOf(user.getUserId()));
        } else{
            return AjaxResult.error("当前角色无新增权限");
        }
        if (ledgerSafetyProductionMeeting.getMeetingTime() != null){
            ledgerSafetyProductionMeeting.setMeetingWeek(DateUtils.dateToWeek2(ledgerSafetyProductionMeeting.getMeetingTime()));
        }
        if (StringUtils.isNotBlank(meetingPath)){
            System.out.println(meetingPath);
        }
        return toAjax(ledgerSafetyProductionMeetingService.insertLedgerSafetyProductionMeeting(ledgerSafetyProductionMeeting));
    }

    /**
     * 修改安全生产领导小组会议记录台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:meeting:edit')")
    @Log(title = "安全生产领导小组会议记录台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            SysUser user = SecurityUtils.getLoginUser().getUser();
            Long stationId = user.getStationId();
            ledgerSafetyProductionMeeting.setStationId(stationId);
            ledgerSafetyProductionMeeting.setUpdateId(String.valueOf(user.getUserId()));
        }
        else{
            return AjaxResult.error("当前角色无修改权限");
        }
        if (ledgerSafetyProductionMeeting.getMeetingTime() != null){
            ledgerSafetyProductionMeeting.setMeetingWeek(DateUtils.dateToWeek2(ledgerSafetyProductionMeeting.getMeetingTime()));
        }
        return toAjax(ledgerSafetyProductionMeetingService.updateLedgerSafetyProductionMeeting(ledgerSafetyProductionMeeting));
    }

    /**
     * 附件上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            String path = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/meeting", file);
            return AjaxResult.success(path);
        }
        return AjaxResult.error("请选择附件");
    }

    /**
     * 删除安全生产领导小组会议记录台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:meeting:remove')")
    @Log(title = "安全生产领导小组会议记录台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ledgerSafetyProductionMeetingService.deleteLedgerSafetyProductionMeetingByIds(ids));
    }


    /**
     * 导出安全生产领导小组会议记录台账详细信息
     */
    @Log(title = "安全生产领导小组会议记录台账", businessType = BusinessType.EXPORT)
    @GetMapping(value = "exportDetail/{id}")
    public AjaxResult exportDetail(@PathVariable("id") Long id)
    {
        return ledgerSafetyProductionMeetingService.exportDetail(id);
    }
}
